#include <iostream>
using namespace std;
#include "connectPool.h"
#include "mysqlDataBase.h"
#include <chrono>

/* 不使用连接池 */
void op1()
{
    const char *host = "localhost";
    const char *user = "root";
    const char *passwd = "1";
    for (int idx = 0; idx < 5000; idx++)
    {
        /* 连接数据库 */
        MysqlDataBase conn(host, user, passwd);
        conn.connectDB("musicDB");

        const char *sql = "insert into userInfo (username,age) values('zhangsan',20);";
        /* 执行sql语句 */
        conn.executeSql(sql);
        /* 关闭数据库 */
        conn.close();
    }
}

/* 使用连接池 */
void op2(ConnectPool *pool)
{
    for (int idx = 0; idx < 5000; idx++)
    {
        std::shared_ptr<MysqlDataBase> conn = pool->getConnection();
        const char *sql = "insert into userInfo (username,age) values('zhangsan',20);";

        conn->executeSql(sql);
    }
}

void test()
{

#if 0
    std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now();
    /* 不使用连接池 */
    op1();
    std::chrono::steady_clock::time_point endTime = std::chrono::steady_clock::now();

    auto interval = endTime - startTime;
    cout <<  interval.count() << endl;
#endif

#if 1
    ConnectPool *pool = ConnectPool::getConnectPool();
    std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now();
    /* 使用连接池 */
    op2(pool);
    std::chrono::steady_clock::time_point endTime = std::chrono::steady_clock::now();
    auto interval = endTime - startTime;
    cout << interval.count() << endl;
#endif
}

int main()
{
    test();
    return 0;
}
